package com.demo.service.impl;

import com.demo.mapper.LogMapper;
import com.demo.po.LogPo;
import com.demo.service.ILogService;
import com.demo.service.common.BizErrorCode;
import com.demo.service.common.BizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.List;

@Component("logService")
public class LogServiceImpl implements ILogService {

    private Logger log = LoggerFactory.getLogger(LogServiceImpl.class);

    @Qualifier("logMapper")
    @Autowired
    private LogMapper logMapper;

    @Override
    public int countLogs(LogPo cond) {
        log.debug("countLogs starting...");
        int count = 0;
        try {
            count = logMapper.countLogs(cond);
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("countLogs end");
        return count;
    }

    @Override
    public List<LogPo> findLogsByPage(LogPo cond) {
        log.debug("findLogsByPage starting...");
        List<LogPo> logs = null;
        try {
            logs = logMapper.findLogsByPage(cond);
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("findLogsByPage end");
        return logs;
    }

    @Override
    public List<LogPo> findAllLogs(LogPo cond) {
        log.debug("findAllLogs starting...");
        List<LogPo> logs = null;
        try {
            logs = logMapper.findAllLogs(cond);
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("findAllLogs end");
        return logs;
    }

    @Override
    public LogPo getLog(Integer id) {
        log.debug("getLog starting...");
        LogPo logPo = null;
        try {
            logPo = logMapper.getLog(id);
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("getLog end");
        return logPo;
    }

    @Override
    public Integer addLog(LogPo obj) {
        log.debug("addLog starting...");
        Integer retval = 0;
        try {
            retval = logMapper.addLog(obj);

            if (retval == 0) {
                throw new BizException(BizErrorCode.EX_SQL_FAIL);
            }
            retval = obj.getId();
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("addLog end");
        return retval;
    }

    @Override
    public int updateLog(LogPo obj) {
        log.debug("updateLog starting...");
        int retval = 0;
        try {
            retval = logMapper.updateLog(obj);
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        if (retval == 0) {
            throw new BizException(BizErrorCode.EX_SQL_FAIL);
        }
        log.debug("updateLog end");
        return retval;
    }

    @Override
    public int deleteLog(Integer id) {
        log.debug("deleteLog starting...");
        int retval = 0;
        try {
            retval = logMapper.deleteLog(id);
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("deleteLog end");
        return retval;
    }
}